package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper,CheckItem> implements CheckItemService {



    @Override
    public Boolean deleteById(Long id) {
        //先查询对象
        CheckItem checkItem = baseMapper.selectById(id);
        if (checkItem != null){
            checkItem.setIs_delete(1);
        }
        return this.updateById(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        return this.list(queryWrapper);
    }

    @Override
    public PageResult findByPage(QueryPageBean pageBean) {

        Page<CheckItem> objectPage = new Page<CheckItem>(pageBean.getCurrentPage(), pageBean.getPageSize());

        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);

        IPage<CheckItem> result = this.page(objectPage,queryWrapper);

        PageResult pageResult = new PageResult(result.getTotal(), result.getRecords());

        return pageResult;
    }



    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        //按照条件查询
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        //过滤已删除
        queryWrapper.eq("is_delete",0);

        //多字段模糊查询 作为一个结果集参与查询
        queryWrapper.and(checkItemQueryWrapper -> {
            checkItemQueryWrapper.like("code", pageBean.getQueryString())
                    .or().like("name",pageBean.getQueryString());
        });

        //分页查询
        Page<CheckItem> objectPage = new Page<CheckItem>(pageBean.getCurrentPage(), pageBean.getPageSize());

        IPage<CheckItem> result = this.page(objectPage,queryWrapper);

        return new PageResult(result.getTotal(), result.getRecords());

    }

    @Override
    public List<CheckItem> findBygroupId(Long id) {
        return baseMapper.findBygroupId(id);
    }


}
